{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4740ec2e",
   "metadata": {
    "notebookRunGroups": {
     "groupValue": "1"
    }
   },
   "source": [
    "随机森林"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cfcbc36c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机森林模型的预测准确率： 0.94\n"
     ]
    }
   ],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.colors import ListedColormap\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "#拆分数据集\n",
    "x,y=load_iris().data[:,2:4],load_iris().target   \n",
    "x_train,x_test,y_train,y_test=train_test_split(x,y, random_state=0,test_size=50)\n",
    "\n",
    "\n",
    "#训练模型\n",
    "model=RandomForestClassifier(n_estimators=10,random_state=0)\n",
    "model.fit(x_train,y_train)\n",
    "\n",
    "\n",
    "#评估模型\n",
    "pred=model.predict(x_test)\n",
    "ac=accuracy_score(y_test,pred)\n",
    "print(f\"随机森林模型的预测准确率：{ac}\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "AIG241",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.14.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
